package com.WOWTradeNews.tablet;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.WOWTradeNewsTest.R;
import com.WOWTradeNews.Utils;
import com.WOWTradeNews.DataBase.News;
import com.WOWTradeNews.DataBase.NewsDataBase;

public class NewsDetail extends Activity implements Runnable {
	public String _id, title, source, date,htmlData,subject;
    private WebView myWebView;
    private NewsDataBase dataBase;
    private ProgressDialog pd;
    private TextView titleTV, sourceTV,dateTV;
    private News news;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.market_research_detail);
        
        Bundle bundle = this.getIntent().getExtras();
        if(bundle!=null){
        	_id = bundle.getString("newsID");
        }
        
        dataBase = new NewsDataBase(this);
        news = dataBase.findByNews(_id);
        
        titleTV = (TextView)findViewById(R.id.title);
        sourceTV = (TextView)findViewById(R.id.source);
        dateTV = (TextView)findViewById(R.id.date);
        
        title = news.getTitle();
        source =news.getSource();
        date = news.getStartDate();
        htmlData = news.getContent2();
        
        if(date != null){
        	date = date.replaceAll("T", " ");
        	
        	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
        	
        	try {
				Date tempDate = df.parse(date);
				date = df2.format(tempDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        titleTV.setText(title);
        sourceTV.setText(source);
        dateTV.setText(date);
        
        if(source == null){
        	sourceTV.setVisibility(View.GONE);
        }
        
		pd = new ProgressDialog(this);
		pd.setMessage("載入中...");
		pd.show();

		new Thread(this).start();
    }

	@Override
	public void run() {
		myWebView = (WebView)findViewById(R.id.myWebView);
		myWebView.setWebViewClient(new WebViewClient(){});
		
		WebSettings websettings = myWebView.getSettings();  
        websettings.setSupportZoom(true);  
        websettings.setBuiltInZoomControls(true);   
        websettings.setJavaScriptEnabled(true);  
        
        final Context myApp = this;        
        myWebView.setWebChromeClient(new WebChromeClient() {  
            @Override  
            public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result){  
                new AlertDialog.Builder(myApp)  
                    .setMessage(message)  
                    .setPositiveButton(android.R.string.ok,  
                            new AlertDialog.OnClickListener() {  
                                public void onClick(DialogInterface dialog, int which)  {  
                                    result.confirm();  
                                }  
                            })  
                    .setCancelable(false)  
                    .create()  
                    .show();  
                return true;  
            };  
        });
        
        htmlData="<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"/></head><body>"+htmlData+"</body></html>";        
        htmlData=htmlData.replaceAll("%", "%25");        
        
        myWebView.loadDataWithBaseURL (null, htmlData, "text/html", "UTF-8",null);
        
        if(news.getAttachmentUrl()!=null && news.getAttachmentUrl().length()>0){
        	Button attachment_btn=(Button)findViewById(R.id.attachment_btn);
        	attachment_btn.setOnClickListener(new OnClickListener(){
        		public void onClick(View view){
        			String url="http://210.241.128.49/App/pdf/"+news.getAttachmentUrl();

        			Intent it = Utils.getPdfFileIntent(url);
        			startActivity(it);        			
        		}
        	});
        }
        else {
        	LinearLayout ll=(LinearLayout)findViewById(R.id.ll);
        	ll.setVisibility(View.INVISIBLE);        	
        }
        
        dataBase.close();

        handler.sendEmptyMessage(0);        
        
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
		}
	};	
}